<template>
  <div style="margin-top: 50px">
     <el-card  class="filter-container" shadow="never" >
      <el-row>
          
            <el-col :span="24" >
             {{this.$route.query.name}} <p v-if="bAnswer">答案</p>
               <div ref="test1"> </div>
                <div ref="test2"> </div>
            </el-col>
      </el-row>
     </el-card>
      <el-card  class="filter-container" shadow="never" v-for = "(q,index) in list" :key="index">
         <el-row >
          
            <el-col :span="2" >
             {{index+1}}
               
            </el-col>
            <el-col :span="2" >
             题库号：{{q.id}}
               
            </el-col>
            <el-col :span="20" >
              <div :ref="`qRef${index}`">
               </div>
            </el-col>
        </el-row>

     
      </el-row>
     </el-card>

    
    
  </div>
</template>

<script>
  import { listQuestions,listAnswers } from '@/api/question';
  import {mapGetters} from 'vuex'

  export default {
    name: "ExamPrint",
  

   
    data() {
      return {
       list: null,
       listLoading: false,
       bAnswer:parseInt(this.$route.query.bAnswer)
      }
    },
    computed: {
      ...mapGetters([
         
           
            'IMG_URL'
          ]),

      
    },
    created() {
      this.getList()

    },
    updated(){
       console.log(this.$refs)
       var length=this.list.length
        for(var i=0;i<length;i++){
          if (this.bAnswer===0){
            this.$refs[`qRef${i}`][0].innerHTML=this.list[i].question
          }else{
            this.$refs[`qRef${i}`][0].innerHTML=this.list[i].answer
          }
        }


     },
    watch: {
      
    },
    methods: {
      //输入是要处理的字符串,返回替换的字符串
      replaceBackImage(toString){
        if (toString===null) return
        var newString=toString.replace(new RegExp("<IMG_URL>","gm"),this.IMG_URL) 

        return newString
         

      },

      //获得试题列表
      getList() {
        this.listLoading = true;
        
        var submitParam={}
        
        submitParam.questionIds=this.$route.query.questionIds
        var i
        if (this.bAnswer===0){
          listQuestions(submitParam).then(response => {
              this.listLoading = false;
              this.list = response.data.list;

              var length=this.list.length
              for(i=0;i<length;i++){
                this.list[i].question=this.replaceBackImage(this.list[i].question)
               
              }
               this.listLoading = false;
           });

        } else {
           listAnswers(submitParam).then(response => {
              this.listLoading = false;
              this.list = response.data.list;

              var length=this.list.length
              for( i=0;i<length;i++){
                this.list[i].answer=this.replaceBackImage(this.list[i].answer)
               
              }
               this.listLoading = false;
            });
        }
        

         

     
      },
     
     
    }
  }
</script>

<style scoped>
 
</style>
